package com.beeantec.jnrerc.modules.user.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.beeantec.jnrerc.core.api.ApiRest;
import com.beeantec.jnrerc.core.api.controller.BaseController;
import com.beeantec.jnrerc.core.api.dto.BaseIdRespDTO;
import com.beeantec.jnrerc.core.api.dto.BaseIdsReqDTO;
import com.beeantec.jnrerc.core.api.dto.PagingReqDTO;
import com.beeantec.jnrerc.dto.MemberVo;
import com.beeantec.jnrerc.dto.MemberWbookDTO;
import com.beeantec.jnrerc.entity.PaperQuAnswer;
import com.beeantec.jnrerc.modules.member.service.MemberService;
import com.beeantec.jnrerc.modules.paper.service.PaperQuAnswerService;
import com.beeantec.jnrerc.modules.user.service.MemberWbookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* <p>
* 错题本控制器
* </p>
*
* @author Anthony
*/
@Api(tags={"错题本"})
@RestController
@RequestMapping("/api/memberwbook")
public class MemberWBookController extends BaseController {

    @Autowired
    private MemberWbookService baseService;

    @Autowired
    private MemberService memberService;


    /**
    * 批量删除
    * @param reqDTO
    * @return
    */
    @ApiOperation(value = "批量删除")
    @RequestMapping(value = "/delete", method = { RequestMethod.POST})
    public ApiRest delete(@RequestBody BaseIdsReqDTO reqDTO) {
        //根据ID删除
        baseService.removeByIds(reqDTO.getIds());
        return super.success();
    }

    /**
    * 分页查找
    * @param reqDTO
    * @return
    */
    @ApiOperation(value = "分页查找")
    @RequestMapping(value = "/paging", method = { RequestMethod.POST})
    public ApiRest<IPage<MemberWbookDTO>> paging(@RequestHeader("token")String token, @RequestBody PagingReqDTO<MemberWbookDTO> reqDTO) {

        //分页查询并转换
        MemberVo memberVo = memberService.token(token);
        reqDTO.setUserId(memberVo.getMemberId().toString());
        IPage<MemberWbookDTO> page = baseService.paging(reqDTO);

        return super.success(page);
    }

    /**
     * 查找列表，每次最多返回200条数据
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "查找列表")
    @RequestMapping(value = "/next", method = { RequestMethod.POST})
    public ApiRest<BaseIdRespDTO> nextQu(@RequestHeader("token")String token,@RequestBody MemberWbookDTO reqDTO) {
        //转换并返回
        MemberVo memberVo = memberService.token(token);
        String quId = baseService.findNext(reqDTO.getExamId(), reqDTO.getQuId(),memberVo.getMemberId().toString());
        return super.success(new BaseIdRespDTO(quId));
    }


    /**
     * 查找列表
     * @return
     */
    @ApiOperation(value = "查找列表")
    @RequestMapping(value = "/list", method = { RequestMethod.GET})
    public ApiRest<List<PaperQuAnswer>> list(@RequestHeader("token")String token,@RequestParam("paperId")Integer paperId,@RequestParam("examId")String examId) {
        MemberVo memberVo = memberService.token(token);
        return super.success(baseService.findPaperError(examId, paperId,memberVo.getMemberId().toString()));
    }
}
